﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebChatSerivce.Models;
using WebChatService.Models;

namespace WebChatSerivce.Controllers
{
    public class FriendConnectionAPIController : ApiController
    {
        //private WebChatEntities db = new WebChatEntities();
        private IDatabase db;

        public FriendConnectionAPIController()
        {
            db = new FriendConnectionEFDB();
        }

        // GET api/FriendConnectionAPI
        public IEnumerable<FriendConnection> GetFriendConnections()
        {
            return (IEnumerable<FriendConnection>)db.GetAllObjects();
        }

        // GET api/FriendConnectionAPI/5
        public FriendConnection GetFriendConnection(int id)
        {
            FriendConnection friendconnection = (FriendConnection)db.GetSingleObject(id);
            if (friendconnection == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return friendconnection;
        }

        // PUT api/FriendConnectionAPI/5
        public HttpResponseMessage PutFriendConnection(int id, FriendConnection friendconnection)
        {
            if (ModelState.IsValid && id == friendconnection.ID)
            {
                db.UpdateObject(friendconnection);

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/FriendConnectionAPI
        public HttpResponseMessage PostFriendConnection(FriendConnection friendconnection)
        {
            if (ModelState.IsValid)
            {
                db.AddObject(friendconnection);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, friendconnection);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = friendconnection.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/FriendConnectionAPI/5
        public HttpResponseMessage DeleteFriendConnection(int id)
        {
            FriendConnection friendconnection = (FriendConnection)db.GetSingleObject(id);
            if (friendconnection == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.RemoveObject(friendconnection);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, friendconnection);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}